package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonRoleMapper;
import com.zhijian.medical.entity.po.PersonRole;
import com.zhijian.medical.entity.vo.request.PersonRoleSearchVo;
import com.zhijian.medical.entity.vo.response.PersonRoleVo;
import com.zhijian.medical.service.PersonRoleService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-角色关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-21 14:32:50
 */
@Slf4j
@Service
public class PersonRoleServiceImpl implements PersonRoleService {

    @Autowired
    private PersonRoleMapper mapper;

    @Override
    public PersonRole selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonRoleVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByPersonId(Long personId) {
        mapper.deleteByPersonId(personId);
    }

    @Override
    public Long insert(PersonRole personRole) {
        Date now = new Date();
        personRole.setCreateTime(now);
        personRole.setUpdateTime(now);
        return mapper.insert(personRole) > 0 ? personRole.getId() : 0L;
    }

    @Override
    public boolean update(PersonRole personRole) {
        Date now = new Date();
        personRole.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personRole) != 0;
    }

    @Override
    public Integer count(PersonRoleSearchVo personRoleSearchVo) {
        return mapper.count(personRoleSearchVo);
    }

    @Override
    public List<PersonRoleVo> selectList(PersonRoleSearchVo personRoleSearchVo) {
        return mapper.selectList(personRoleSearchVo);
    }

    @Override
    public Page<PersonRoleVo> selectPage(PersonRoleSearchVo personRoleSearchVo) {
        //先直接查总数
        int count = count(personRoleSearchVo);
        //分页
        Page<PersonRoleVo> page = new Page<>(true, count, personRoleSearchVo.getPageNum(), personRoleSearchVo.getPageSize());
        personRoleSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonRoleVo> responseVos = this.selectList(personRoleSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public List<Long> selectRolesByPersonId(Long personId) {
        return mapper.selectRolesByPersonId(personId);
    }

}